{% extends "inventory/base.html" %}
{% load inventory_tags %}

{% block title %}会员分析 - 报表中心{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-md-8">
            <h1 class="h2">会员分析报表</h1>
            <p class="text-muted">查看会员等级分布与消费行为分析</p>
        </div>
        <div class="col-md-4 text-md-end">
            <div class="btn-group">
                <a href="{% url 'reports_index' %}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left me-1"></i> 返回报表中心
                </a>
                <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#exportModal">
                    <i class="bi bi-file-excel me-1"></i> 导出Excel
                </button>
            </div>
        </div>
    </div>

    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">日期筛选</h5>
                </div>
                <div class="card-body">
                    <form method="post" class="row g-3">
                        {% csrf_token %}
                        <div class="col-md-4">
                            <label class="form-label">开始日期</label>
                            {{ form.start_date }}
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">结束日期</label>
                            {{ form.end_date }}
                        </div>
                        <div class="col-md-4 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="bi bi-search me-1"></i> 查询
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 会员概要统计 -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body py-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <i class="bi bi-people-fill" style="font-size: 2.5rem;"></i>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h5 class="mb-0">{{ member_data.total_members }}</h5>
                            <div class="small">会员总数</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body py-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <i class="bi bi-person-plus-fill" style="font-size: 2.5rem;"></i>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h5 class="mb-0">{{ member_data.new_members }}</h5>
                            <div class="small">新增会员数</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body py-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <i class="bi bi-activity" style="font-size: 2.5rem;"></i>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h5 class="mb-0">{{ member_data.active_members }}</h5>
                            <div class="small">活跃会员数</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body py-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <i class="bi bi-percent" style="font-size: 2.5rem;"></i>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h5 class="mb-0">{{ member_data.activity_rate|floatformat:1 }}%</h5>
                            <div class="small">会员活跃率</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row mb-4">
        <!-- 会员等级分布 -->
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">会员等级分布</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>会员等级</th>
                                    <th class="text-end">会员数</th>
                                    <th class="text-end">占比</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for level in member_data.level_distribution %}
                                <tr>
                                    <td>{{ level.level__name }}</td>
                                    <td class="text-end">{{ level.count }}</td>
                                    <td class="text-end">{{ level.count|divisor:member_data.total_members|floatformat:1 }}%</td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="3" class="text-center">暂无数据</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- 会员与非会员消费对比 -->
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">会员与非会员消费对比</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>客户类型</th>
                                    <th class="text-end">消费金额</th>
                                    <th class="text-end">订单数</th>
                                    <th class="text-end">平均客单价</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>会员</td>
                                    <td class="text-end">¥{{ member_data.member_sales.total_amount|default:"0.00" }}</td>
                                    <td class="text-end">{{ member_data.member_sales.total_count|default:"0" }}</td>
                                    <td class="text-end">¥{{ member_data.member_avg_order|floatformat:2 }}</td>
                                </tr>
                                <tr>
                                    <td>非会员</td>
                                    <td class="text-end">¥{{ member_data.non_member_sales.total_amount|default:"0.00" }}</td>
                                    <td class="text-end">{{ member_data.non_member_sales.total_count|default:"0" }}</td>
                                    <td class="text-end">¥{{ member_data.non_member_avg_order|floatformat:2 }}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 消费会员排行 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">消费会员排行 (TOP 10)</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>排名</th>
                                    <th>会员姓名</th>
                                    <th>会员等级</th>
                                    <th>手机号</th>
                                    <th class="text-end">消费金额</th>
                                    <th class="text-end">消费次数</th>
                                    <th class="text-end">平均客单价</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for member in member_data.top_members %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td>{{ member.name }}</td>
                                    <td>
                                        <span class="badge bg-info text-dark">{{ member.level.name }}</span>
                                    </td>
                                    <td>{{ member.phone }}</td>
                                    <td class="text-end">¥{{ member.period_spend|floatformat:2 }}</td>
                                    <td class="text-end">{{ member.period_purchase_count }}</td>
                                    <td class="text-end">¥{{ member.period_spend|div:member.period_purchase_count|floatformat:2 }}</td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="7" class="text-center py-3">暂无数据</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 导出模态框 -->
    <div class="modal fade" id="exportModal" tabindex="-1" aria-labelledby="exportModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exportModalLabel">导出报表</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form method="post">
                    {% csrf_token %}
                    <input type="hidden" name="export_excel" value="1">
                    <input type="hidden" name="start_date" value="{{ start_date|date:'Y-m-d' }}">
                    <input type="hidden" name="end_date" value="{{ end_date|date:'Y-m-d' }}">
                    
                    <div class="modal-body">
                        <p>您将导出以下时间范围的会员分析报表：</p>
                        <div class="alert alert-info">
                            {{ start_date|date:"Y年m月d日" }} 至 {{ end_date|date:"Y年m月d日" }}
                        </div>
                        <p>导出的Excel文件将包含：</p>
                        <ul>
                            <li>会员总览统计数据</li>
                            <li>会员等级分布</li>
                            <li>会员消费排行</li>
                        </ul>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-success">
                            <i class="bi bi-file-excel me-1"></i> 确认导出
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 为输入字段添加css类
        document.querySelector('input[name="start_date"]').classList.add('form-control');
        document.querySelector('input[name="end_date"]').classList.add('form-control');
    });
</script>
{% endblock %} 